# -*- python -*-
# Copyright (c) 2014 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

Import('env')

def AddTest(env, src, exit_status='0', golden_file=None, test_suffix='',
            is_broken=False, compile_env=None, link_env=None,
            EXTRA_LIBS=[]):
  name = src.split('.')[0] + test_suffix

  compile_env = compile_env or env
  link_env = link_env or env

  obj = compile_env.ComponentObject(name, src)
  nexe = link_env.ComponentProgram(
      name, obj, EXTRA_LIBS=['${NONIRT_LIBS}'] + EXTRA_LIBS)
  node = env.CommandSelLdrTestNacl(
      name + '.out', nexe, exit_status=exit_status,
      stdout_golden=golden_file)
  env.AddNodeToTestSuite(
      node, ['toolchain_tests', 'small_tests'], 'run_' + name + '_test',
      is_broken=is_broken)

c_flags = ['-std=gnu99']
cxx_flags = (['-Wc++11-narrowing', '-std=gnu++11']
             if env.Bit('bitcode') else ['-std=gnu++0x'])

native_env = env.Clone()
if env.Bit('bitcode'):
  native_flags = []
elif env.Bit('build_x86_32'):
  native_flags = ['-msse2', '-msse3', '-msse4']
elif env.Bit('build_x86_64'):
  native_flags = ['-msse2', '-msse3', '-msse4']
elif env.Bit('build_arm'):
  native_flags = ['--target=armv7a-unknown-nacl-gnueabi','-mfloat-abi=hard']
elif env.Bit('build_mips32'):
  native_flags = []
native_env.Append(CFLAGS=c_flags)
native_env.Append(CXXFLAGS=cxx_flags)
native_env.Append(CCFLAGS=native_flags)
# C++ code can't use vector literals because of the following warning:
#   compound literals are a C99-specific feature
# Remove pedantic to work around this issue.
native_env.FilterOut(CCFLAGS=['-pedantic'])

ref_env = env.Clone()
ref_flags = ['-DVREFIMPL']
ref_env.Append(CFLAGS=c_flags)
ref_env.Append(CXXFLAGS=cxx_flags)
ref_env.Append(CCFLAGS=ref_flags)

# TODO(jfb): The native environment currently doesn't compile.
if env.Bit('bitcode'):
  AddTest(native_env, 'simd.cc', test_suffix='_native')
AddTest(ref_env, 'simd.cc', test_suffix='_ref')

# GCC 4.4 doesn't support all features of the vector extensions in C.
# TODO(jfb) Re-enable testing when we migrate to a newer GCC.
if env.Bit('bitcode') and env['TOOLCHAIN_FEATURE_VERSION'] >= 5:
  AddTest(native_env, 'vector_align.cc',
          golden_file=native_env.File('vector_align.stdout'),
          EXTRA_LIBS=['${PTHREAD_LIBS}'])
  AddTest(native_env, 'vector_alloca.cc',
          golden_file=native_env.File('vector_alloca.stdout'),
          EXTRA_LIBS=['${PTHREAD_LIBS}'])
  AddTest(native_env, 'vector_extension.c',
          golden_file=native_env.File('vector_extension.stdout'))
  AddTest(native_env, 'vector_shuffle.cc',
          golden_file=native_env.File('vector_shuffle.stdout'))
